package com.example.touch.time;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import com.example.touch.communication.entities.DayTime;
import com.example.touch.communication.entities.DayTime.DayOfWeek;
import com.example.touch.communication.entities.TimeTable;

public class EvaluableTimeTable implements IEvaluableTimeTable {

    private Date from;
    private Date to;    
    private ArrayList<DayTime> days; 
    
    public EvaluableTimeTable(TimeTable timeTable)
    {
    	this.from = timeTable.getFrom();
    	this.to = timeTable.getTo();
    	this.days = timeTable.getDays();
    }
	
	@Override
	public boolean isWithin(Calendar now) {
		if(now.before(from))
            return false;
        if(now.after(to))
            return false;
        
        int iDayOfWeek = now.get(Calendar.DAY_OF_WEEK);
        DayOfWeek dof = toEnum(iDayOfWeek);
        
        for(DayTime dt : days) {
            if(dt.getDay() == dof) {
                
                Calendar startDate = (Calendar) now.clone();
                Calendar endDate = (Calendar) now.clone();
                
                startDate.set(Calendar.HOUR_OF_DAY, dt.GetStartTime().getHour());
                startDate.set(Calendar.MINUTE, dt.GetStartTime().getMinute());
                endDate.set(Calendar.HOUR_OF_DAY, dt.getEndTime().getHour());
                endDate.set(Calendar.MINUTE, dt.getEndTime().getMinute());
                
                if(now.after(startDate) && now.before(endDate)) {
                    return true;
                } else {
                    return false;
                }
                
            }
        }
        return false;      
	}

    public DayOfWeek toEnum(int dayOfWeek) {
            switch(dayOfWeek) {
                case Calendar.MONDAY:
                    return DayOfWeek.monday;
                case Calendar.TUESDAY:
                    return DayOfWeek.tuesday;
                case Calendar.WEDNESDAY:
                    return DayOfWeek.wednesday;
                case Calendar.THURSDAY:
                    return DayOfWeek.thursday;
                case Calendar.FRIDAY:
                    return DayOfWeek.friday;
                case Calendar.SATURDAY:
                    return DayOfWeek.saturday;
                case Calendar.SUNDAY:
                    return DayOfWeek.sunday;
                default:
                    return DayOfWeek.monday;
        }
    }
}
